package com.education.welco.controller.app;


import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.*;
import com.education.welco.pojo.vo.ScatalogClassVO;
import com.education.welco.pojo.vo.ScatalogVO;
import com.education.welco.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 教师创建课程 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@Api(tags = {"APP-学校课程详情"})
@RequestMapping("/app/sCatalog")
public class AppSCatalogController extends BaseController {

    @Autowired
    private ISCatalogService isCatalogService;

    @Autowired
    private ITTeacherService teacherService;

    @Autowired
    private ISResourcesService resourcesService;

    @Autowired
    private ISCatalogClassService isCatalogClassService;

    @Autowired
    private ISCatalogMenuService isCatalogMenuService;

    /**
     * 根据(班级id与当天日期)去查询某一天的教学课表
     *
     * @param pageNum
     * @param pageSize
     * @param classId     班级id
     * @param toDayString 查询某天的课表   '1997-10-07' 标准参数
     * @return
     */
    @ApiOperation(value = "app查询某一天的教学课表", httpMethod = "GET")
    @GetMapping("/selectClassSchedule")
    public ApiReturnObject selectClassSchedule(Integer pageNum, Integer pageSize, Integer classId, String toDayString, String keyWord) {
        if (classId == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        } else {
            //开启分页
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            //需要日期格式的处理 '1997-10-07'
            String nowString = "";
            try {
                //Date date = new SimpleDateFormat("yyyy年MM月dd日").parse("2022年6月20日");
                Date date = new SimpleDateFormat("yyyy年MM月dd日").parse(toDayString);//解析为日期
                nowString = new SimpleDateFormat("yyyy-MM-dd").format(date);//格式化为String
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //直播课的状态修改
            int i = isCatalogService.updateLiveCourseStatus();
            List<ScatalogClassVO> sds = isCatalogService.findClassScheduleByClassId(classId, nowString, keyWord);
            /*关于两个字段判断为一个字段的逻辑处理
            isLz 直播课 还是普通课  （1：开启录制，0不开启录制）
            status 直播课的状态  0 未开始  1直播中 2录播结束
            finalStatus:专业课0 未直播1 直播中2 直播结束(录播)3*/
            for (ScatalogClassVO scatalogClassVO : sds) {
                //处理时间格式
                //第一层判断 (直播课 还是普通课)
                if (scatalogClassVO.getIsLz().equals("1")) {
                    //第二层判断
                    switch (scatalogClassVO.getStatus()) {
                        case "0":
                            scatalogClassVO.setFinalStatus("1");
                            break;
                        case "1":
                            scatalogClassVO.setFinalStatus("2");
                            break;
                        case "2":
                            scatalogClassVO.setFinalStatus("3");
                            break;
                    }
                } else {
                    //设置专业课状态
                    scatalogClassVO.setFinalStatus("0");
                }
            }
            PageInfo<ScatalogClassVO> scatalogClassVOPageInfo = new PageInfo<>(sds);
            return new ApiReturnObject("200", "查询成功", scatalogClassVOPageInfo);
        }
    }

    public static void main(String[] args) throws ParseException {
        Date date = new SimpleDateFormat("yyyy年MM月dd日").parse("2022年6月20日");
        String now = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println(now);
    }

    /**
     * 根据班级id与关键词 查询app端全部课程列表
     *
     * @param pageNum
     * @param pageSize
     * @param classId  班级id
     * @param keyWord  查询关键词
     * @return
     */
    @ApiOperation(value = "app查询全部课程列表", httpMethod = "GET")
    @GetMapping("/selectAllList")
    public ApiReturnObject selectAllList(Integer pageNum, Integer pageSize, Integer classId, String keyWord) {
        if (null == classId) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        } else {
            //开启分页
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            //直播课的状态修改
            int i = isCatalogService.updateLiveCourseStatus();
            List<ScatalogClassVO> sds = isCatalogService.findCourseByClassId(classId, keyWord);
            //课程状态数据处理方便app状态展示
            if (sds.size() != 0) {
            /*关于两个字段判断为一个字段的逻辑处理
            isLz 直播课 还是普通课  （1：开启录制，0不开启录制）
            status 直播课的状态  0 未开始  1直播中 2录播结束
            finalStatus:专业课0 未直播1 直播中2 直播结束(录播)3*/
                for (ScatalogClassVO scatalogClassVO : sds) {
                    //第一层判断 (直播课 还是普通课)
                    if (scatalogClassVO.getIsLz().equals("1")) {
                        //第二层判断
                        switch (scatalogClassVO.getStatus()) {
                            case "0":
                                scatalogClassVO.setFinalStatus("1");
                                break;
                            case "1":
                                scatalogClassVO.setFinalStatus("2");
                                break;
                            case "2":
                                scatalogClassVO.setFinalStatus("3");
                                break;
                        }
                    } else {
                        //设置专业课状态
                        scatalogClassVO.setFinalStatus("0");
                    }
                }
            }
            //分页信息
            PageInfo<ScatalogClassVO> scatalogClassVOPageInfo = new PageInfo<>(sds);
            return new ApiReturnObject("200", "查询成功", scatalogClassVOPageInfo);
        }
    }

    @ApiOperation(value = "查询学校课程", httpMethod = "GET")
    @GetMapping("/selectList")
    public ApiReturnObject selectList(Integer catalogState, Integer userId, Integer classId) {
        if (classId == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        } else {
            ScatalogVO scatalogVO = new ScatalogVO();
//          scatalogVO.setUserId(userId);
            scatalogVO.setCatalogState(catalogState);
            scatalogVO.setClassId(classId);
            List<SCatalog> catalogList = Lists.newArrayList();
            if (catalogState != null) {
                if (1 == catalogState) {
                    //在授    根据班级
                    catalogList = isCatalogService.findByUserIdAndClassId2(scatalogVO);
                } else if (2 == catalogState) {
                    //完结
                    catalogList = isCatalogService.findByUserIdAndClassId3(scatalogVO);
                }
            } else {
                //只有班级id
                catalogList = isCatalogService.findByUserIdAndClassId(scatalogVO);
            }
            for (SCatalog catalog : catalogList) {
                //查询录播视频
                SResources sResources = new SResources();
                sResources.setRCatalogid(catalog.getId());
                sResources.setType("9");
                List<SResources> resourcesList = resourcesService.selectSResourcesList(sResources);
                if (resourcesList.size() != 0) {
                    catalog.setCreateTime(resourcesList.get(0).getCreateTime());
                    catalog.setCatalogUrl(resourcesList.get(0).getRUrl());
                }
                //设置老师信息
                setTeacherInformation(catalog);
            }
            return new ApiReturnObject("200", "查询成功", catalogList.size(), catalogList);
        }
    }

    /**
     * 设置老师信息
     *
     * @param catalog
     */
    private void setTeacherInformation(SCatalog catalog) {
        //格式化日期  创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String format = null;
        if (catalog.getCreateTime() != null) {
            format = sdf.format(catalog.getCreateTime());
        }
        catalog.setSimpleCreateTime(format);

        //设置目录表id
        catalog.setCatalogMenuId(catalog.getParentId());

        //查询老师信息
        if (catalog.getCatalogTeacherid() == null || "".equals(catalog.getCatalogTeacherid())) {
            catalog.setTeacherName("老师");//老师姓名
            catalog.setTeacherIntro("精英讲师");//老师简介
        } else {
            TTeacher tTeacher = teacherService.selectTTeacherById(Integer.parseInt(catalog.getCatalogTeacherid()));
            if (tTeacher == null || "".equals(tTeacher)) {
                catalog.setTeacherName("老师");//老师姓名
                catalog.setTeacherIntro("精英讲师");//老师简介
            } else {
                catalog.setTeacherName(tTeacher.getTeacherName());//老师姓名
                catalog.setTeacherIntro(tTeacher.getTeacherIntro());//老师简介
            }
        }
    }


    @ApiOperation(value = "查询学校课程的id", httpMethod = "GET")
    @GetMapping("/selectCatalogId")
    public ApiReturnObject selectCatalogId(Integer userId, Integer classId) {
        if (classId == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        }
        List<Integer> list = new ArrayList<>();
        SCatalogClass catalogClass = new SCatalogClass();
//        catalogClass.setUserId(userId);
        catalogClass.setClassId(classId);
        List<SCatalogClass> classList = isCatalogClassService.selectSCatalogClassList(catalogClass);
        for (SCatalogClass sCatalogClass : classList) {
            list.add(sCatalogClass.getCatalogId());
        }
        return new ApiReturnObject("200", "查询成功", list.size(), list);
    }


    @ApiOperation(value = "学校课程--视频接口", httpMethod = "GET")
    @GetMapping("/selectByVideo")
    public ApiReturnObject selectByVideo(Integer catalogId, Integer classId) {
        if (null != catalogId) {
            SCatalog sCatalog = isCatalogService.selectSCatalogById(catalogId);
            if (sCatalog == null) {
                //安卓端要求返回码为200
                return new ApiReturnObject("200", "没有该课程", "没有该课程");
            }
            //格式化日期  创建时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            String format = null;
            if (sCatalog.getCreateTime() != null) {
                format = sdf.format(sCatalog.getCreateTime());
            }
            //设置目录表id
            sCatalog.setCatalogMenuId(sCatalog.getParentId());

            sCatalog.setSimpleCreateTime(format);
            SResources sResources = new SResources();
            sResources.setRCatalogid(sCatalog.getParentId());
            //1 教学课件  2 技术文档  3 教学笔记 4 图片 5 视频
            sResources.setType("5");
            sResources.setIsStu("1");//是否允许学生查看 1 是 0 否
            sResources.setClassId(classId);
            List<SResources> resourcesList = resourcesService.selectSResourcesList(sResources);
            sCatalog.setResources(resourcesList);
            return new ApiReturnObject("200", "查询成功", sCatalog);
        } else {
            return new ApiReturnObject("201", "catalogId为空", "catalogId为空");
        }
    }


    @ApiOperation(value = "学校课程--目录筛选接口 安卓", httpMethod = "GET")
    @GetMapping("/selectByMenu")
    public ApiReturnObject selectByMenu(Integer catalogMenuId, Integer userId, Integer classId) {
        if (userId == null || classId == null || catalogMenuId == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        }
        //判断该目录是否是第三级目录
        List<SCatalogMenu> sCatalogMenus = isCatalogMenuService.selectSCatalogMenuByParentId(catalogMenuId);
        if (sCatalogMenus.size() != 0) {
            return new ApiReturnObject("202", "请选择第三级目录进行查询", "请选择第三级目录进行查询");
        }

        //根据第三级目录id查询该课程
        ScatalogVO scatalogVO = new ScatalogVO();
        scatalogVO.setUserId(userId);
        scatalogVO.setCatalogMenuId(catalogMenuId);
        scatalogVO.setClassId(classId);
        List<SCatalog> sCatalog = isCatalogService.findByUserIdAndClassId(scatalogVO);
        //格式化日期  创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String format = null;
        if (sCatalog.size() != 0) {
            if (sCatalog.get(0).getCreateTime() != null) {
                format = sdf.format(sCatalog.get(0).getCreateTime());
            }
            //设置目录表id
            sCatalog.get(0).setCatalogMenuId(sCatalog.get(0).getParentId());

            sCatalog.get(0).setSimpleCreateTime(format);
            SResources sResources = new SResources();
            sResources.setRCatalogid(sCatalog.get(0).getId());
            sResources.setType("5");
            List<SResources> resourcesList = resourcesService.selectSResourcesList(sResources);
            sCatalog.get(0).setResources(resourcesList);
        } else {
            return new ApiReturnObject("201", "未查询到三级目录", sCatalog);
        }
        return new ApiReturnObject("200", "查询成功", sCatalog);
    }


    @ApiOperation(value = "学校课程--目录筛选接口 IOS", httpMethod = "GET")
    @GetMapping("/selectByMenu2")
    public ApiReturnObject selectByMenu2(Integer menuId) {
        if (menuId == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        }
        List<SCatalog> catalogList = isCatalogService.findByMenuId(menuId);
        for (SCatalog catalog : catalogList) {
            setTeacherInformation(catalog);
        }
        return new ApiReturnObject("200", "查询成功", catalogList);
    }

    /*    @ApiOperation(value = "学校课程--目录筛选接口", httpMethod = "GET")
        @GetMapping("/selectByVideo2")
        public ApiReturnObject selectByVideo2(Integer catalogMenuId) {
            if (null != catalogMenuId) {
                SCatalog catalog = new SCatalog();
                catalog.setParentId(catalogMenuId);
                List<SCatalog> sCatalog = isCatalogService.selectSCatalogList(catalog);
                //格式化日期  创建时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                String format = null;
                if (sCatalog.get(0).getCreateTime() != null) {
                    format = sdf.format(sCatalog.get(0).getCreateTime());
                }
                //设置目录表id
                sCatalog.get(0).setCatalogMenuId(sCatalog.get(0).getParentId());

                sCatalog.get(0).setSimpleCreateTime(format);
                SResources sResources = new SResources();
                sResources.setRCatalogid(sCatalog.get(0).getId());
                sResources.setType("5");
                List<SResources> resourcesList = resourcesService.selectSResourcesList(sResources);
                sCatalog.get(0).setResources(resourcesList);
                return new ApiReturnObject("200","查询成功",sCatalog);
            } else {
                return new ApiReturnObject("201", "catalogId为空", "catalogId为空");
            }
        }*/
}